<template>
  <el-dialog width="600px" :title="dialogTitle" :visible.sync="dialogVisible" append-to-body @close="handleClose">
    <el-form
      ref="submitForm"
      v-loading="formLoading"
      :model="submitForm"
      :rules="submitFormRules"
      label-width="80px"
      label-position="right"
      class="submit-form"
    >
      <el-form-item label="材料" prop="materialName">
        <el-autocomplete
          v-model="submitForm.materialName"
          class="inline-input"
          :fetch-suggestions="selectMaterialList"
          placeholder="请输入材料"
          style="width: 100%"
          clearable
          @select="handleSelectMaterial"
        >
          <template slot-scope="{ item }">
            <div>{{ item.name }}</div>
          </template>
          <el-button
            slot="append"
            icon="el-icon-search"
            @click="openMaterialDialog"
          />
        </el-autocomplete>
      </el-form-item>
      <el-form-item label="数量" prop="quantity">
        <el-input v-model="submitForm.quantity" type="number" placeholder="请输入数量" />
      </el-form-item>
      <el-form-item label="单位" prop="unit">
        <dict-select v-model="submitForm.unit" dict-key="bom_unit" placeholder="请选择单位" />
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button :loading="formLoading" type="text" @click="handleClose">取消</el-button>
      <el-button :loading="formLoading" type="primary" @click="handleSubmit">提交</el-button>
    </span>
    <div v-if="materialVisible">
      <material-module :visible="materialVisible" @close="closeMaterialDialog" />
    </div>
  </el-dialog>
</template>

<script>
import { saveBom, updateBomById, selectBomById } from '@/api/erp/bom'
import { selectMaterialByName } from '@/api/erp/material'
import materialModule from '@/modules/materialModule.vue'
/**
 * @author yizuomin
 * @date 2023/5/7 19:05
 **/
export default {
  name: 'SubmitModule',
  components: { materialModule },
  props: {
    visible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      dialogVisible: this.visible,
      dialogTitle: '',
      formLoading: false,
      materialVisible: false,
      submitForm: {
        id: '',
        materialName: '',
        unit: '',
        materialId: '',
        productId: '',
        quantity: ''
      },
      submitFormRules: {
        materialName: [{ required: true, message: '请输入材料名称', trigger: 'change' }],
        unit: [{ required: true, message: '请选择单位', trigger: 'change' }],
        quantity: [{ required: true, message: '请输入数量', trigger: 'change' }],
        unitPrice: [{ required: true, message: '请输入含税单价', trigger: 'change' }]
      }
    }
  },
  methods: {
    init(productId, id) {
      this.submitForm.productId = productId
      if (!id) {
        this.dialogTitle = '新增物料清单'
        return
      }
      this.dialogTitle = '修改物料清单'
      this.formLoading = true
      selectBomById(id).then(resp => {
        if (resp.success) {
          this.submitForm = resp.data
        }
      }).finally(() => {
        this.formLoading = false
      })
    },
    handleSubmit() {
      this.$refs.submitForm.validate((valid) => {
        if (valid) {
          let request = saveBom
          if (this.submitForm.id) {
            request = updateBomById
          }
          this.formLoading = true
          request(this.submitForm).then(resp => {
            if (resp.success) {
              this.$message.success('操作成功')
              this.$refs.submitForm.resetFields()
              this.handleClose()
            }
          }).finally(() => {
            this.formLoading = false
          })
        }
      })
    },
    handleClose() {
      this.$emit('close')
    },
    selectMaterialList(query, callback) {
      selectMaterialByName({ name: query }).then(resp => {
        if (resp.success) {
          callback(resp.data)
        }
      })
    },
    // 选中
    handleSelectMaterial(row) {
      this.submitForm.materialId = row.id
      this.submitForm.materialName = row.name
      this.submitForm.unit = row.unit
      this.submitForm.unitPrice = row.unitPrice
    },
    // 打开弹窗
    openMaterialDialog() {
      this.materialVisible = true
    },
    // 关闭弹窗
    closeMaterialDialog(row) {
      if (row) {
        this.submitForm.materialId = row.id
        this.submitForm.materialName = row.name
        this.submitForm.unit = row.unit
        this.submitForm.unitPrice = row.unitPrice
      }
      this.materialVisible = false
    }
  }
}
</script>

<style scoped>

</style>
